//
//  HTtabbar.swift
//  mySinaBlog
//
//  Created by whtapple on 3/21/16.
//  Copyright © 2016 whtapple. All rights reserved.
//

import UIKit
class HTtabbar: UITabBar {
    let buttenCount = 5
    //闭包
    var composeCallback: (() -> ())?
    override func layoutSubviews() {
        //设置宽、高、Y 值
        let barBtnW = frame.width / CGFloat(buttenCount)
        let barBtnH = frame.height
        let barBtnY = 0
//        print("subViews: \(subviews)")
        //初始化设置控件下标为0
        var index = 0
        let origionFrame = CGRect(x:CGFloat(0), y: CGFloat(barBtnY), width: barBtnW, height: barBtnH)
        //遍历子控件进行布局
        // MARK: - 标记
        for view in subviews {
            let cls = NSClassFromString("UITabBarButton")
            //当子控件为这个类的时候布局
            if view.isKindOfClass(cls!){
                let barBtnX = CGFloat(index) * barBtnW
                view.frame = CGRectOffset(origionFrame, barBtnX, CGFloat(0))
                index++
                if index == 2{
                    index++
                }
            }
        }
        composeButton.frame = CGRect(x: CGFloat(2) * barBtnW, y: CGFloat(barBtnY), width: barBtnW, height: barBtnH)
    }
    //加号点击事件方法
    func didClickComposeBtn(){
        self.composeCallback?()
    }
    //mark: - 事件
    /// 懒加载
    private lazy var composeButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Selected)
        button.addTarget(self, action: "didClickComposeBtn", forControlEvents: UIControlEvents.TouchUpInside)
        self.addSubview(button)
        return button
    }()
    
}
